﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Runtime.Remoting.Contexts;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

namespace CinemabookingWCFApp.MovieInfoFolder
{
    [DataContract]
    public class MovieInfoService : IMovieInfoService
    {
        #region
        CinemabookingDBDataContext db = new CinemabookingDBDataContext();
        #endregion

        public List<MovieInfo> GetMovieById(int MovId)
        {         
            var queryResult = (from x in db.MovieInfos
                               where x.id == MovId
                               select x);
            return queryResult.ToList();         
        }

        public List<string> GetMovieByName(string MovName)
        {            
            var queryResult = (from x in db.MovieInfos
                               where x.name == MovName
                               select x.picture);

            return queryResult.ToList();            
        }

        public List<MovieInfo> GetAllMovies()
        {
            var queryResult = (from x in db.MovieInfos
                               orderby x.id
                               select x);
                return queryResult.ToList();                   
        }               

        public void SaveMovie(string MovName, string MovGenre, string MovType, string MovDescription, string MovPicture, int MovTheater, string MovTrailer)
        {
            MovieInfo newMov = new MovieInfo { name = MovName, genre = MovGenre, type = MovType, description = MovDescription, picture = MovPicture, theaterId = MovTheater, trailer = MovTrailer };
            db.MovieInfos.InsertOnSubmit(newMov);
            db.SubmitChanges();
        }

        public void DeleteMovie(int MovId)
        {
            MovieInfo delMov = (from x in db.MovieInfos
                                where x.id == MovId
                                select x).FirstOrDefault();
            if (delMov !=null)
            {
                db.MovieInfos.DeleteOnSubmit(delMov);
                db.SubmitChanges();
            }
        }

        public void UpdateMovie(int MovId, string MovName, string MovGenre, string MovType, string MovDescription, string MovPicture, int MovTheater, string MovTrailer)
        {
            MovieInfo updMov = (from x in db.MovieInfos
                                where x.id == MovId
                                select x).Single();

            updMov.name = MovName;
            updMov.genre = MovGenre;
            updMov.type = MovType;
            updMov.description = MovDescription;
            updMov.picture = MovPicture;
            updMov.theaterId = MovTheater;
            updMov.trailer = MovTrailer;

            db.SubmitChanges();
        }
    }
}
